package cn.ds.assets.module.assets.controller.admin.metadataModelManage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.metadataModelManage.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataModelManage.BaseMetadataModelManageDO;
import cn.ds.assets.module.assets.convert.metadataModelManage.BaseMetadataModelManageConvert;
import cn.ds.assets.module.assets.service.metadataModelManage.BaseMetadataModelManageService;

//@Tag(name = "管理后台 - 元数据管理-元模型-管理属性")
//@RestController
//@RequestMapping("/assets/base-metadata-model-manage")
@Validated
public class BaseMetadataModelManageController {

    @Resource
    private BaseMetadataModelManageService baseMetadataModelManageService;

    @PostMapping("/create")
    @Operation(summary = "创建元数据管理-元模型-管理属性")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-manage:create')")
    public CommonResult<Long> createBaseMetadataModelManage(@Valid @RequestBody BaseMetadataModelManageCreateReqVO createReqVO) {
        return success(baseMetadataModelManageService.createBaseMetadataModelManage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新元数据管理-元模型-管理属性")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-manage:update')")
    public CommonResult<Boolean> updateBaseMetadataModelManage(@Valid @RequestBody BaseMetadataModelManageUpdateReqVO updateReqVO) {
        baseMetadataModelManageService.updateBaseMetadataModelManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除元数据管理-元模型-管理属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-manage:delete')")
    public CommonResult<Boolean> deleteBaseMetadataModelManage(@RequestParam("id") Long id) {
        baseMetadataModelManageService.deleteBaseMetadataModelManage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得元数据管理-元模型-管理属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-manage:query')")
    public CommonResult<BaseMetadataModelManageRespVO> getBaseMetadataModelManage(@RequestParam("id") Long id) {
        BaseMetadataModelManageDO baseMetadataModelManage = baseMetadataModelManageService.getBaseMetadataModelManage(id);
        return success(BaseMetadataModelManageConvert.INSTANCE.convert(baseMetadataModelManage));
    }

    @GetMapping("/list")
    @Operation(summary = "获得元数据管理-元模型-管理属性列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-manage:query')")
    public CommonResult<List<BaseMetadataModelManageRespVO>> getBaseMetadataModelManageList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseMetadataModelManageDO> list = baseMetadataModelManageService.getBaseMetadataModelManageList(ids);
        return success(BaseMetadataModelManageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得元数据管理-元模型-管理属性分页")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-manage:query')")
    public CommonResult<PageResult<BaseMetadataModelManageRespVO>> getBaseMetadataModelManagePage(@Valid BaseMetadataModelManagePageReqVO pageVO) {
        PageResult<BaseMetadataModelManageDO> pageResult = baseMetadataModelManageService.getBaseMetadataModelManagePage(pageVO);
        return success(BaseMetadataModelManageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出元数据管理-元模型-管理属性 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-manage:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseMetadataModelManageExcel(@Valid BaseMetadataModelManageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseMetadataModelManageDO> list = baseMetadataModelManageService.getBaseMetadataModelManageList(exportReqVO);
        // 导出 Excel
        List<BaseMetadataModelManageExcelVO> datas = BaseMetadataModelManageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "元数据管理-元模型-管理属性.xls", "数据", BaseMetadataModelManageExcelVO.class, datas);
    }

}
